package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class StringTest extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField str;
	private JLabel lblNewLabel_1;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JButton btnNewButton;
	private JLabel lblNewLabel_2;
	private JTextField test;
	private JButton btnNewButton_1;
	private JButton btnIgnorecase;
	private JButton btnIndexof;
	private JButton btnLength;
	private JButton btnReplace;
	private JButton btnReplaceall;
	private JButton btnSplit;
	private JButton btnSubstring;
	private JButton btnLastindexof;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StringTest frame = new StringTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StringTest() {
		setTitle("문자열 테스트");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 739, 417);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getStr());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getTest());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getBtnIgnorecase());
		contentPane.add(getBtnIndexof());
		contentPane.add(getBtnLength());
		contentPane.add(getBtnReplace());
		contentPane.add(getBtnReplaceall());
		contentPane.add(getBtnSplit());
		contentPane.add(getBtnSubstring());
		contentPane.add(getBtnLastindexof());
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("원본 문자열");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(23, 27, 78, 15);
		}
		return lblNewLabel;
	}
	private JTextField getStr() {
		if (str == null) {
			str = new JTextField();
			str.setBounds(114, 24, 464, 21);
			str.setColumns(10);
		}
		return str;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("결과");
			lblNewLabel_1.setBounds(44, 220, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(23, 245, 627, 117);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("charAt");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int pos = Integer.parseInt(test.getText());
						char c = str.getText().charAt(pos);
						result.append("\n");
						result.append("charAt = "+c);
					}catch(Exception ex){
						result.setText("테스트 문자항목에는 숫자를 입력하거나 숫자가 너무 큽니다.");
						test.setText("");
						test.requestFocus();
					}
				}
			});
			btnNewButton.setBounds(36, 86, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("테스트 문자열");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_2.setBounds(12, 60, 89, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getTest() {
		if (test == null) {
			test = new JTextField();
			test.setBounds(114, 54, 464, 21);
			test.setColumns(10);
		}
		return test;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("contains");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						if(str.getText().contains(test.getText())
							&& !test.getText().equals("")){
							result.append("\n 포함되어 있음");
						} else {result.append("\n 포함되어있지 않음");
					    }
					}catch(Exception ex){
						result.append("\n 문자열을 확인하세요");
					}
					
				}
			});
			btnNewButton_1.setBounds(145, 86, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnIgnorecase() {
		if (btnIgnorecase == null) {
			btnIgnorecase = new JButton("ignoreCase");
			btnIgnorecase.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						if(str.getText().equalsIgnoreCase(test.getText())){
							result.append("\n문자열과 동일함");
						}else{result.append("\n문자열과 동일하지 않음");
						}
					}catch(Exception ex){
						result.append("\n 문자열을 확인하세요");
					}
				}
			});
			btnIgnorecase.setBounds(254, 86, 97, 23);
		}
		return btnIgnorecase;
	}
	private JButton getBtnIndexof() {
		if (btnIndexof == null) {
			btnIndexof = new JButton("IndexOf");
			btnIndexof.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int pos = str.getText().indexOf(test.getText());
							 result.append("\n"+pos);
						
					}catch(Exception ex){
						result.append("\n문자열을 확인하세요");
					}
				}
			});
			btnIndexof.setBounds(363, 86, 97, 23);
		}
		return btnIndexof;
	}
	private JButton getBtnLength() {
		if (btnLength == null) {
			btnLength = new JButton("Length");
			btnLength.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int pos = str.getText().length();
						 result.append("\n"+pos);
					}catch(Exception ex){
						result.append("\n문자열을 확인하세요");
					}				       
					
				}
			});
			btnLength.setBounds(36, 119, 97, 23);
		}
		return btnLength;
	}
	private JButton getBtnReplace() {
		if (btnReplace == null) {
			btnReplace = new JButton("Replace");
			btnReplace.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String a = str.getText().replace(test.getText(), "*");
						// test.getText() 대신 *을 넣는다.
						result.append("replace : " + a + "\n");

					} catch (Exception e) {
						result.append("replace 실행중 오류");
					}				
				}
			});
			btnReplace.setBounds(145, 119, 97, 23);
		}
		return btnReplace;
	}
	private JButton getBtnReplaceall() {
		if (btnReplaceall == null) {
			btnReplaceall = new JButton("ReplaceAll");
			btnReplaceall.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String a = str.getText().replaceAll(test.getText(), "&");
					result.append("replaceAll : " + a + "\n");
				}
			});
			btnReplaceall.setBounds(254, 119, 97, 23);
		}
		return btnReplaceall;
	}
	private JButton getBtnSplit() {
		if (btnSplit == null) {
			btnSplit = new JButton("Split");
			btnSplit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String[] k= str.getText().split(test.getText());
					result.append("\n"+Arrays.toString(k));
				}
			});
			btnSplit.setBounds(363, 119, 97, 23);
		}
		return btnSplit;
	}
	private JButton getBtnSubstring() {
		if (btnSubstring == null) {
			btnSubstring = new JButton("SubString");
			btnSubstring.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String temp = str.getText();
					String r = temp.substring(2,4);
					 result.append("substring:"+r);
				}
			});
			btnSubstring.setBounds(472, 119, 106, 23);
		}
		return btnSubstring;
	}
	private JButton getBtnLastindexof() {
		if (btnLastindexof == null) {
			btnLastindexof = new JButton("LastIndexOf");
			btnLastindexof.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						int pos = str.getText().lastIndexOf(test.getText());
						result.append("\n lastIndexOf : " + pos);
					} catch (Exception e) {
						result.append("lastIndexOf 처리중 오류");
					}
				
				
				}
			});
			btnLastindexof.setBounds(472, 86, 106, 23);
		}
		return btnLastindexof;
	}
}
